<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("Writer.php");
//Includes
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtinputvalidator.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("buttons.inc.php");
use_unit("components4phpfull/jtiframe.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Vehicle_List extends Page
{
       public $lblcount = null;
       public $lblDownApp = null;
       public $btnPrint = null;
       public $hfDec = null;
       public $lblDecommission = null;
       public $Image5 = null;
       public $Label2 = null;
       public $Image3 = null;
       public $Label1 = null;
       public $JTDivInfo = null;
       public $JTGroupBox5 = null;
       public $Label13 = null;
       public $imgInfo = null;
       public $JSAlertChassisFirst = null;
       public $btnSearch = null;
       public $edtSearch = null;
       public $Image4 = null;
       public $btnExport = null;
       public $JTVehicleHidden = null;
       public $JTJavaScript1 = null;
       public $Image2 = null;
       public $JTPlatinumVehicleList = null;
       public $hiddenVeh = null;
       public $Image1 = null;
       public $JTSiteTheme1 = null;
       function lblDownAppJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       window.open("lib/GLOBE_AMPS_INSTALLER.exe","Window1");
       <?php

       }

       function btnPrintJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnPrint'].src="image/btn_print.png";
       <?php

       }

       function btnPrintJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnPrint'].src="image/btn_print2.png";
       <?php

       }

       function btnPrintJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var dec = document.getElementById('hfDec').value;

       if(dec==1)
       window.open('print_decommissioned.php','popup','width=850,height=768,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0');

       if(dec==0)
       window.open('print_active.php','popup','width=850,height=768,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0');

       <?php

       }

       function lblDecommissionClick($sender, $params)
       {
       $hfDec = $this->hfDec->getValue();

       if($hfDec!=1)
       {
       redirect('vehicle_list.php?dec=1');
       }
       if($hfDec==1)
       {
       redirect('vehicle_list.php?dec=0');
       }

       }

       function imgInfoJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivInfo').Show();
       <?php

       }

       function edtSearchJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       var search = document.getElementById('edtSearch').value;

       if(search=='')
       {
       document.getElementById('edtSearch').value='search...';
       }

       if(search!='')
       {
       document.getElementById('edtSearch').value=search;
       }

       <?php

       }




       function btnExportJSClick($sender, $params)
       {

       ?>
       //var dec = document.getElementById('hfDec').value;
       //window.open("timely_report_company.php?dec="+dec,"Window2","menubar=no,width=430,height=360,left=450,top=250,toolbar=no");
       window.open('select_dynamic_report.php','_blank','menubar=no,width=300,height=180,left=480,top=280,toolbar=no');
       <?php

       }


       function btnExportJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExport'].src="image/btn_dynamic_reports.png"
       <?php

       }

       function btnExportJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExport'].src="image/btn_dynamic_reports2.png"
       <?php

       }

       function JTVehicleHiddenShow($sender, $params)
       {
        $comp_id = $_SESSION['company_id'];
        if($this->hfDec->getValue()!='1')
        $query_hidden = "select * from vehicle_tbl where out_of_service='0' AND company_id='".$comp_id."'";
        if($this->hfDec->getValue()=='1')
        $query_hidden = "select * from vehicle_tbl where out_of_service='1' AND company_id='".$comp_id."'";
        $result_hidden = mysql_query($query_hidden)or die(mysql_error());

        $arr = array();
        while($row_hidden = mysql_fetch_array($result_hidden))
          {
            $vehicle_id = $row_hidden['vehicle_id'];
            $track_by = $row_hidden['track_by'];

            if($track_by=='distance')
            {
            $curr_tracking = $row_hidden['curr_tracking_value'];
            $lbl = 'Km';
            }

            if($track_by=='hours')
            {
            $curr_tracking = $row_hidden['curr_hours'];
            $lbl = 'Hrs';
            }

            /*if($this->hiddenVeh->getValue()==$vehicle_id)
            $radio = "<a href=vehicle_list.php?veh=".$vehicle_id."><img src='image/radio_select.png' border></a>";
            else*/
            //$radio = "<a href=vehicle_list.php?veh=".$vehicle_id."><img src='image/radio.png' border></a>";
            $VIN = "<a href=vehicle_edit.php?veh=".$vehicle_id." title='edit vehicle'>".$row['VIN']."</a>";
            $tools2 = "<a href=veh_details.php?veh=".$vehicle_id."><img src='image/details_orange.jpg' title='additional details' no border></a>
            <a href=details_emission.php?veh=".$vehicle_id."><img src='image/inspection_orange.jpg' title='emission/inspection/journal' no border></a>
            <a href=veh_fuel_list.php?veh=".$vehicle_id."><img src='image/fuel_orange.jpg' title='fuel log' no border></a>
            <a href=veh_workorder_list.php?veh=".$vehicle_id."><img src='image/tools_orange.jpg' title='maintenance/work order' no border></a>
            <a href=veh_expense_list.php?veh=".$vehicle_id."><img src='image/expense_orange.jpg' title='payments/fees/expense' no border></a>
            <img onclick=js_delete('".$vehicle_id."'); src='image/delete_orange.jpg' title='delete'>";

            $arr[] = array(
                        //'VIN' => $row_hidden['VIN'],
                        'Plate Number' => $row_hidden['veh_num'],
                        'Year' => $row_hidden['year'],
                        'Make' => $row_hidden['manufacturer_name'],
                        'Model' => $row_hidden['model_name'],
                        'Km/Hrs' => $curr_tracking.$lbl
                        //'Type/Style' => $row_hidden['style_name']
                        );


          }

            $this->JTVehicleHidden->setCellData($arr);


       }

       function btnExportClick($sender, $params)
       {
       //$this->JTVehicleHidden->exportGridToXLSDownload('vehicle_list.xls');
       window.open('main_dynamic_report.php','_blank');

       }



       function Vehicle_ListJSLoad($sender, $params)
       {

       ?>
       //Add your javascript code here
       var cap = document.getElementById('lblDecommission').innerHTML;
       document.getElementById('lblDecommission').innerHTML = "<span style='text-decoration:underline'>"+cap+"</span>";

       document.getElementById('JTPlatinumVehicleList_colitem_hdr_0').width = '128';
       document.getElementById('JTPlatinumVehicleList_colitem_hdr_1').width = '128';
       document.getElementById('JTPlatinumVehicleList_colitem_hdr_2').width = '128';
       document.getElementById('JTPlatinumVehicleList_colitem_hdr_3').width = '138';
       document.getElementById('JTPlatinumVehicleList_colitem_hdr_4').width = '138';
       document.getElementById('JTPlatinumVehicleList_colitem_hdr_5').width = '138';
       document.getElementById('JTPlatinumVehicleList_colitem_hdr_6').width = '230';

       document.getElementById('JTPlatinumVehicleList_colitem_body_0').width = '128';
       document.getElementById('JTPlatinumVehicleList_colitem_body_1').width = '128';
       document.getElementById('JTPlatinumVehicleList_colitem_body_2').width = '128';
       document.getElementById('JTPlatinumVehicleList_colitem_body_3').width = '138';
       document.getElementById('JTPlatinumVehicleList_colitem_body_4').width = '138';
       document.getElementById('JTPlatinumVehicleList_colitem_body_5').width = '138';
       document.getElementById('JTPlatinumVehicleList_colitem_body_6').width = '230';

       <?php

       }

       function Image1JSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['Image1'].src="image/add_new.png"
       <?php

       }

       function Image1JSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['Image1'].src="image/add_new_blue.png"
       <?php

       }

       function JTPlatinumVehicleList($sender, $params)
       {
        $comp_id = $_SESSION['company_id'];
        $query1 = "select * from vehicle_tbl where company_id='".$comp_id."' and out_of_service=0 and (VIN like '%".$this->edtSearch->Text."%'
        or veh_num like '%".$this->edtSearch->Text."%'
        or year like '%".$this->edtSearch->Text."%'
        or manufacturer_name like '%".$this->edtSearch->Text."%'
        or model_name like '%".$this->edtSearch->Text."%'
        or style_name like '%".$this->edtSearch->Text."%'
        or location like '%".$this->edtSearch->Text."%'
        or department like '%".$this->edtSearch->Text."%'
        or curr_tracking_value like '%".$this->edtSearch->Text."%') order by vehicle_id ASC";

        if($this->hfDec->getValue()==1)
        {
        $query1 = "select * from vehicle_tbl where company_id='".$comp_id."' and out_of_service=1 and (VIN like '%".$this->edtSearch->Text."%'
        or veh_num like '%".$this->edtSearch->Text."%'
        or year like '%".$this->edtSearch->Text."%'
        or manufacturer_name like '%".$this->edtSearch->Text."%'
        or model_name like '%".$this->edtSearch->Text."%'
        or style_name like '%".$this->edtSearch->Text."%'
        or style_name like '%".$this->edtSearch->Text."%'
        or department like '%".$this->edtSearch->Text."%') order by vehicle_id ASC";
        }

        $result = mysql_query($query1)or die(mysql_error());


        $arr = array();
        while($row = mysql_fetch_array($result))
          {
            $vehicle_id = $row['vehicle_id'];
            $oos = $row['out_of_service'];

            $query_decommission = "select * from veh_decommission where veh_id='".$vehicle_id."' order by autoid DESC limit 1";
            $result_decom = mysql_query($query_decommission)or die(mysql_error());
            $row_decom = mysql_fetch_array($result_decom);
            $reason = $row_decom['reason'];



            if($oos==1)
            {
            $color = "Black";
            }

            if($oos==0)
            {
            $color = "Black";
            }

            //$pic = $row['photo'];



            /*if($pic=='')
            {
            $photo = "<div align='center'><a href=vehicle_edit.php?veh=".$vehicle_id."><img src=image/no image.png height='50px' width='50px' title='edit'></a></div>";
            }*/



            if($this->hiddenVeh->getValue()==$vehicle_id)
            $radio = "<a href=vehicle_list.php?veh=".$vehicle_id."><img src='image/radio_select.png' border></a>";
            else
            //$radio = "<a href=vehicle_list.php?veh=".$vehicle_id."><img src='image/radio.png' border></a>";
            $VIN = "<a href=vehicle_edit.php?veh=".$vehicle_id." title='edit vehicle'>".$row['veh_num']."</a>";

            $enc_vehicle_id = base64_encode($vehicle_id);

            $query_chassis_tbl_exist = "SELECT id FROM veh_chassis_tbl WHERE vehicle_id='".$vehicle_id."'";
            $result_chassis_tbl_exist = mysql_query($query_chassis_tbl_exist) or die("VEH CHASSIS TBL EXIST ".mysql_error());
            $chassis_tbl_exist = mysql_num_rows($result_chassis_tbl_exist);

            if($chassis_tbl_exist>0)
            $manage_tires = "<a href=manage_tires.php?vin=".$vehicle_id."><img src='image/tire_veh.png' title='tire mount/dismount' no border></a>";
            if($chassis_tbl_exist==0)
            $manage_tires = "<img style='cursor:pointer;' onclick=chassisFirst(); src='image/tire_veh.png' title='tire mount/dismount' no border>";
            if($_SESSION['access_vehicle']==1)
            {
            $tools2 = "<a href=veh_details.php?veh=".$vehicle_id."><img src='image/add_details.png' title='additional details' border='0'></a>
            <a href=veh_fuel_list.php?veh=".$vehicle_id."><img src='image/fuel.png' title='fuel list' no border></a>
            <a href=trouble_workorder_list.php?veh_id=".$vehicle_id."><img src='image/trouble.png' title='trouble & work order' no border></a>
            <a href=veh_workorder_list.php?veh=".$vehicle_id."><img src='image/maintenance.png' title='work summary' no border></a>
            <a href=check_reminder.php?veh=".$vehicle_id."><img src='image/check_reminder.png' title='reminder list' no border></a>
            <a href=incident_flow.php?veh=".$vehicle_id."><img src='image/incident.png' title='incident list' no border></a><br/>
            ".$manage_tires."
            <a href=details_emission2.php?veh=".$vehicle_id."><img src='image/expense.png' title='emission/other expenses' no border></a>
            <a href=veh_decommission_list.php?veh=".$vehicle_id."><img src='image/out_of_service.png' title='decommission history' no border></a>
            <a href=veh_insurance_list.php?veh=".$vehicle_id."><img src='image/veh_insurance.png' title='insurance' no border></a>";
            }

            //<img onclick=pop_report('".$enc_vehicle_id."'); src='image/reports.png' title='excel reports' style='cursor:pointer'>

            if($_SESSION['access_vehicle']==2)
            {
            $tools2 = "<a href=veh_details.php?veh=".$vehicle_id."><img src='image/add_details.png' title='additional details' border='0'></a>
            <a href=veh_fuel_list.php?veh=".$vehicle_id."><img src='image/fuel.png' title='fuel list' no border></a>
            <a href=trouble_workorder_list.php?veh_id=".$vehicle_id."><img src='image/trouble.png' title='trouble & work order' no border></a>
            <a href=veh_workorder_list.php?veh=".$vehicle_id."><img src='image/maintenance.png' title='work summary' no border></a>
            <a href=check_reminder.php?veh=".$vehicle_id."><img src='image/check_reminder.png' title='reminder list' no border></a>
            <img onclick=js_delete('".$vehicle_id."'); src='image/delete.png' title='delete' style='cursor:pointer'><br/>
            <a href=incident_flow.php?veh=".$vehicle_id."><img src='image/incident.png' title='incident list' no border></a>
            ".$manage_tires."
            <a href=details_emission2.php?veh=".$vehicle_id."><img src='image/expense.png' title='emission/other expenses' no border></a>
            <a href=veh_decommission_list.php?veh=".$vehicle_id."><img src='image/out_of_service.png' title='decommission history' no border></a>
            <a href=veh_insurance_list.php?veh=".$vehicle_id."><img src='image/veh_insurance.png' title='insurance' no border></a>";
            }


            //<img onclick=pop_report('".$enc_vehicle_id."'); src='image/reports.png' title='excel reports' style='cursor:pointer'>
            //<img onclick=pop_report('".$enc_vehicle_id."'); src='image/reports.png' title='excel reports' style='cursor:pointer'>
            //<a href=tire_list.php?veh=".$vehicle_id."><img src='image/tire.png' title='tire management' no border></a>

            $img_name = 'img_details'.$vehicle_id;

            $details = "<a href=veh_details.php?veh=".$vehicle_id."><img id='".$img_name."' name='".$img_name."' src='image/add_details.png' title='additional details' border='0'>";
            //$details = "<a href=veh_details.php?veh=".$vehicle_id."><img id='".$img_name."' name='".$img_name."' src='image/add_details.png' title='additional details' border='0' onmouseover=document.getElementById('".$img_name."').src='image/add_details2.png' onmouseout=document.getElementById('".$img_name."').src='image/additional_details.png'></a>";
            //<a href=timely_report.php?veh=".$vehicle_id."><img src='image/reports.png' title='generate reports' no border></a>
            $track = $row['track_by'];

            if($track=='distance')
            {
            $od1 = $row['tracking_value'];
            $od2 = $row['curr_tracking_value'];
            $mileage = $od2;
            $mil = $mileage.'Km';
            }

            if($track=='hours')
            {
            $od1 = $row['start_hours'];
            $od2 = $row['curr_hours'];
            $mileage = $od2;
            $mil = $mileage.'Hours';
            }


            $photo = "<div align='center'><a href=vehicle_edit.php?veh=".$vehicle_id."><img src='".$row['photo']."' height='50px' width='50px' title='edit'></a></div>";
            /*if($_SESSION['isAdmin']==1)
            $vphoto = $photo;
            if($_SESSION['isAdmin']!=1)
            $vphoto = "<div align='center'><img src='".$row['photo']."' height='75px' width='75px' title='edit'></div>";*/


            if($this->hfDec->getValue()==1)
            {
            $arr[] = array(
                        'Photo' => $photo,
                        'Plate Number' => "<font style='text-transform: uppercase; color:".$color."'>".$row['veh_num']."</font>",
                        'Year' => "<p style='color:".$color."'>".$row['year']."</p>",
                        'Make' => "<p style='color:".$color."'>".$row['manufacturer_name']."</p>",
                        'Model' => "<p style='color:".$color."'>".$row['model_name']."</p>",
                        //'Type/Style' => $row['style_name'],
                        'Reason' =>"<p style='color:".$color."'>".$row_decom['reason']."</p>",
                        'Services' => $tools2
                        );
             }

             if($this->hfDec->getValue()==0)
            {
            $arr[] = array(
                        'Photo' => $photo,
                        'Plate Number' => "<font style='text-transform: uppercase; color:".$color."'>".$row['veh_num']."</font>",
                        'Year' => "<p style='color:".$color."'>".$row['year']."</p>",
                        'Make' => "<p style='color:".$color."'>".$row['manufacturer_name']."</p>",
                        'Model' => "<p style='color:".$color."'>".$row['model_name']."</p>",
                        //'Type/Style' => $row['style_name'],
                        'Total Mileage' =>"<p style='color:".$color."'>".$mil."</p>",
                        'Services' => $tools2
                        );
             }

          }

            $this->JTPlatinumVehicleList->setCellData($arr);



       }

       function edtSearchJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('edtSearch').value = '';

       <?php

       }


       function Vehicle_ListCreate($sender, $params)
       {
       $comp_id = $_SESSION['company_id'];
       $veh = $_GET['veh'];
       $dec = $_GET['dec'];

       if($dec!=null && $dec!='')
       $this->hfDec->setValue($dec);

       $this->hiddenVeh->setValue($veh);
       $this->Image1->setLink('vehicle_create.php?veh='.$veh);

       $result_active = mysql_query("select * from vehicle_tbl where company_id='".$_SESSION['company_id']."' and out_of_service='0'");
       $active_count = mysql_num_rows($result_active);

       $this->lblcount->Caption = $active_count.' active vehicles';

       $result_vehicle = mysql_query("select * from vehicle_tbl where company_id='".$_SESSION['company_id']."' and out_of_service='1'");
       $veh_count = mysql_num_rows($result_vehicle);

       if($this->hfDec->getValue()!=1)
       {
       $this->Image2->ImageSource = 'image/list_vehicle.png';
       $this->imgInfo->setVisible(true);
       $this->btnPrint->Hint = 'Print list of active vehicle/s';
       $this->btnExport->Hint = 'Send list of active vehicle/s to Excel';
       if($veh_count>=1)
       {
       $this->lblDecommission->Caption = 'You have '.$veh_count.' out of service vehicle/s.';
       $this->lblDecommission->Hint = 'Click here to view decommissioned vehicles';
       //$this->lblDecommission->setLink('veh_dec_list.php?company_id='.$_SESSION['company_id']);
       $this->lblDecommission->Font->Color = 'Red';
       $this->lblDecommission->Enabled = true;
       }
       if($veh_count==0)
       {
       $this->lblDecommission->Caption = '';
       $this->lblDecommission->Enabled = false;
       }
       }

       if($this->hfDec->getValue()==1)
       {
       $this->lblDecommission->Caption = 'View active vehicles';
       $this->lblDecommission->Hint = 'Click here to view active vehicles';
       $this->Image2->ImageSource = 'image/decommissioned_veh.png';
       $this->imgInfo->setVisible(false);
       $this->btnPrint->Hint = 'Print list of decommissioned vehicle/s';
       $this->btnExport->Hint = 'Send list of decommissioned vehicle/s to Excel';
       }


       //$this->JTPlatinumVehicleList->Header=center;

       /*if($_SESSION['access_vehicle']==1)
       {
        $this->Image1->setVisible(false);
        $this->btnExport->Left = 951;
        $this->btnPrint->Left = 878;
       }

       if($_SESSION['access_vehicle']==2)
       {
        $this->Image1->setVisible(true);
        $this->btnExport->Left = 847;
        $this->btnPrint->Left = 774;
       }*/

       }

}

global $application;

global $Vehicle_List;

//Creates the form
$Vehicle_List=new Vehicle_List($application);

//Read from resource file
$Vehicle_List->loadResource(__FILE__);

//Shows the form
$Vehicle_List->show();

?>